package com.nf.health.app.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;

import com.nf.health.app.R;


/**
 * 
 * 类功能描述：通用自定义对话框</br>
 *
 * @author Sun ZhaoYong
 * @version 1.0
 * </p>
 * 修改时间：</br>
 * 修改备注：</br>
 */
public class DialogFactory extends Dialog {
	public static final int ID_BTN_CANCEL = R.id.btn_middle;
	public static final int ID_BTN_DEFAULT = R.id.btn_right;
	public static final int ID_BTN_OK = R.id.btn_left;
	private ListView a;
	private ListView b;
	private ArrayAdapter c;
	public Button mBtnCancel;
	public Button mBtnDefault;
	public Button mBtnOK;
	public LinearLayout mBtnsBar;
	public LinearLayout mContent;
	public LinearLayout mContents;
	public Context mContext;
	public LinearLayout mDialog_single_list_item;
	public LinearLayout mDialog_title_bar;
	public View mDivider;
	// public LinearLayout mDivider_top;
	public LayoutInflater mInflater;
	public LinearLayout mListItem;
	public TextView mMsg;
	public ScrollView mScrollView;
	public View mSpaceView;
	public TextView mTitle;
	public ImageView mTitleIcon;
	public ProgressBar mProgress;
	public TextView mTickText;
	public TextView mProgressPercent;
	public TextView mFraction;

	public DialogFactory(Context context) {
		super(context, R.style.dialog2);
		setContentView(R.layout.dialog_factory);
		mContext = context;
		mInflater = getLayoutInflater();
		mTitle = (TextView) findViewById(R.id.dialog_factory_title);
		mMsg = (TextView) findViewById(R.id.dialog_factory_msg);
		mContents = (LinearLayout) findViewById(R.id.dialog_factory_content);
		mBtnOK = (Button) findViewById(R.id.btn_left);
		mBtnCancel = (Button) findViewById(R.id.btn_middle);
		mBtnDefault = (Button) findViewById(R.id.btn_right);
		mSpaceView = mInflater.inflate(R.layout.dialog_space, null);
		mDivider = mInflater.inflate(R.layout.divider, null);
		mBtnsBar = (LinearLayout) findViewById(R.id.btns_bar);
		mDialog_title_bar = (LinearLayout) findViewById(R.id.dialog_title_bar);
		mContent = (LinearLayout) findViewById(R.id.dialog_content);
		mListItem = (LinearLayout) findViewById(R.id.dialog_list_item);
		mScrollView = (ScrollView) findViewById(R.id.scrollview);
		mDialog_single_list_item = (LinearLayout) findViewById(R.id.dialog_single_list_item);
		setCanceledOnTouchOutside(false);
	}

	public DialogFactory(Context context, int title) {
		this(context);
		if (title > 0)
			setTitle(title);
	}

	public DialogFactory(Context context, int title, int message) {
		this(context);
		if (title > 0)
			setTitle(title);
		if (message > 0)
			setMsg(message);
	}

	public DialogFactory(Context context, CharSequence title) {
		this(context);
		if (title != null) {
			setTitle(title);
		} else {
			mDialog_title_bar.setVisibility(8);
		}
	}

	public DialogFactory(Context context, CharSequence title,
			CharSequence message) {
		this(context);
		setTitle(title);
		setMsg(message);
	}

	public View addDivider() {
		return mInflater.inflate(R.layout.divider, mContents);
	}

	public View addView(int layout) {
		View view = mInflater.inflate(layout, null);
		mContents.removeAllViews();
		mContents.addView(view);
		return view;
	}

	public void addView(View view) {
		mContents.removeAllViews();
		mContents.addView(view);
	}

	public void enableMsgProgressBar() {
		View inflated=((ViewStub) findViewById(R.id.dialog_factory_progressbar)).inflate();
		/*mProgress = (ProgressBar) inflated.findViewById(R.id.progress1);
		mTickText = (TextView) inflated.findViewById(R.id.isCleaning);
		mProgressPercent = (TextView) inflated.findViewById(R.id.progressPercent);
		mFraction = (TextView) inflated.findViewById(R.id.cleanedCount);*/
	}
	
	public void setProgress(int progress){
		mProgress.setProgress(progress);
	}
	
	/**
	 * 设置进进度条信息
	 * @param progress 当前的进度值
	 * @param tickText 当前进度的提示信息
	 * @param fraction 分数，例如：2/3
	 */
	public void setProgress(int progress,String tickText,String fraction){
		mProgress.setProgress(progress);
		mTickText.setText(tickText);
		mProgressPercent.setText(progress+"%");
		mFraction.setText(fraction);
	}

	public int getCheckedItemPosition() {
		int i;
		if (b != null)
			i = b.getCheckedItemPosition();
		else
			i = a.getCheckedItemPosition();
		return i;
	}

	public ListView getListView() {
		ListView listview;
		if (b != null)
			listview = b;
		else
			listview = a;
		return listview;
	}

	public void hideMsgView() {
		mMsg.setVisibility(View.GONE);
	}

	public void setButtonOnClickListener(int id,
			View.OnClickListener onclicklistener) {
		switch (id) {
		case R.id.btn_left:
			mBtnOK.setOnClickListener(onclicklistener);
			break;
		case R.id.btn_middle:
			mBtnCancel.setOnClickListener(onclicklistener);
			break;
		case R.id.btn_right:
			mBtnDefault.setOnClickListener(onclicklistener);
			break;
		}
	}

	public void setAllButtonGone() {
		mBtnOK.setVisibility(View.GONE);
		mBtnCancel.setVisibility(View.GONE);
		mBtnDefault.setVisibility(View.GONE);
	}

	public void setButtonText(int id, int btnText) {
		switch (id) {
		case R.id.btn_left:
			mBtnOK.setText(btnText);
			break;
		case R.id.btn_middle:
			mBtnCancel.setText(btnText);
			break;
		case R.id.btn_right:
			mBtnDefault.setText(btnText);
			break;
		}
	}

	public void setButtonText(int id, CharSequence btnText) {
		switch (id) {
		case R.id.btn_left:
			mBtnOK.setText(btnText);
			break;
		case R.id.btn_middle:
			mBtnCancel.setText(btnText);
			break;
		case R.id.btn_right:
			mBtnDefault.setText(btnText);
			break;
		}
	}

	public void setButtonVisibility(int id, boolean flag) {
		int visibility = View.VISIBLE;
		if (!flag) {
			visibility = View.GONE;
		}
		switch (id) {
		case R.id.btn_left:
			mBtnOK.setVisibility(visibility);
			break;
		case R.id.btn_middle:
			mBtnCancel.setVisibility(visibility);
			break;
		case R.id.btn_right:
			mBtnDefault.setVisibility(visibility);
			break;
		}
	}

	public void setContentHeight(int height) {
		View view = (View) mContents.getParent();
		android.view.ViewGroup.LayoutParams layoutparams = view
				.getLayoutParams();
		layoutparams.height = height;
		view.setLayoutParams(layoutparams);
	}

	public void setItems(String as[],
			android.widget.AdapterView.OnItemClickListener onitemclicklistener) {
		mContent.setVisibility(8);
		mListItem.setVisibility(0);
		a = (ListView) findViewById(R.id.listview);
		c = new ArrayAdapter(mContext, R.layout.universal_dialog_list_item, as);
		a.setAdapter(c);
		a.setOnItemClickListener(onitemclicklistener);
	}

	public void setItems(int resourceId,
			android.widget.AdapterView.OnItemClickListener onitemclicklistener) {
		String[] as = mContent.getResources().getStringArray(resourceId);
		mContent.setVisibility(8);
		mListItem.setVisibility(0);
		a = (ListView) findViewById(R.id.listview);
		c = new ArrayAdapter(mContext, R.layout.universal_dialog_list_item, as);
		a.setAdapter(c);
		a.setOnItemClickListener(onitemclicklistener);
	}

	public void setMsg(int message) {
		mMsg.setText(message);
	}

	public void setMsg(CharSequence message) {
		mMsg.setText(message);
	}

	public void setSingleChoiceItems(String as[], int i,
			android.widget.AdapterView.OnItemClickListener onitemclicklistener) {
		mScrollView.setVisibility(View.GONE);
		mDialog_single_list_item.setVisibility(View.VISIBLE);
		b = (ListView) findViewById(R.id.single_listview);
		c = new ArrayAdapter(mContext, R.layout.single_choice_item, as);
		b.setAdapter(c);
		b.setChoiceMode(1);
		if (onitemclicklistener != null)
			b.setOnItemClickListener(onitemclicklistener);
		if (i >= 0)
			b.setItemChecked(i, true);
	}

	public void setTitle(int title) {
		mTitle.setText(title);
	}

	public void setTitle(CharSequence title) {
		mTitle.setText(title);
	}

	public void setTitleIcon(int i) {
		mTitle.setCompoundDrawablesWithIntrinsicBounds(i, 0, 0, 0);
	}

	public void setTitleIcon(Drawable drawable) {
		mTitle.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null,
				null);
	}
	public void setButtonColor(int id,int color){
		switch (id) {
		case R.id.btn_left:
			mBtnOK.setBackgroundColor(color);
			break;
		case R.id.btn_middle:
			mBtnCancel.setBackgroundColor(color);
			break;
		case R.id.btn_right:
			mBtnDefault.setBackgroundColor(color);
			break;
		}
	}
	public void show() {
		super.show();
	}

	public void setButtonOnClickListener(int btnLeft,
			OnClickListener onClickListener) {
		// TODO Auto-generated method stub
		
	}
}


